import {
  buildBatchActions,
  createPageTemplateConfig,
  PageTemplate,
  type ActionButtonConfig,
  type BatchActionConfig,
} from '@/components';
import { t } from '@/components/utils';
import { DeleteOutlined, DownloadOutlined, EditOutlined, EyeOutlined, PoweroffOutlined } from '@ant-design/icons';
import { message } from 'antd';
import { getPageConfig, type DemoRecord } from './pageConfig';

const ConfigDemo = () => {
  const pageConfig = getPageConfig(t);

  // 统一的操作处理函数
  const handleAction = (actionType: string, record: DemoRecord) => {
    console.log('Action triggered:', actionType, record);

    switch (actionType) {
      case 'view':
        message.info(t('demo', 'actionViewDetail', { name: record.name }));
        break;
      case 'edit':
        message.info(t('demo', 'actionEditUser', { name: record.name }));
        break;
      case 'copy':
        message.success(t('demo', 'actionCopyUser', { name: record.name }));
        break;
      case 'send':
        message.success(t('demo', 'actionSendMessage', { name: record.name }));
        break;
      case 'delete':
        message.warning(t('demo', 'actionDeleteUser', { name: record.name }));
        break;
      default:
        message.error(t('demo', 'actionUnknown', { action: actionType }));
    }
  };

  // 自定义操作按钮配置
  const customActions: ActionButtonConfig[] = [
    {
      key: 'view',
      label: t('demo', 'actionView'),
      icon: <EyeOutlined />,
      type: 'link',
      className: 'page-template-action-detail-btn',
      permission: 'view',
    },
    {
      key: 'edit',
      label: t('demo', 'actionEdit'),
      icon: <EditOutlined />,
      type: 'link',
      className: 'page-template-action-edit-btn',
      permission: 'update',
      disabled: (record: DemoRecord) => record.status === 'inactive',
    },
    {
      key: 'delete',
      label: t('demo', 'actionDelete'),
      icon: <DeleteOutlined />,
      type: 'link',
      danger: true,
      className: 'page-template-action-delete-btn',
      permission: 'delete',
      disabled: (record: DemoRecord) => record.role === 'admin',
    },
  ];

  // 批量操作配置
  const batchActionConfigs: BatchActionConfig[] = [
    {
      key: 'delete',
      label: t('demo', 'batchDelete'),
      icon: <DeleteOutlined />,
      danger: true,
      action: async (selectedRows: DemoRecord[]) => {
        console.log(
          '自定义批量删除:',
          selectedRows.map((r) => r.id),
        );
      },
    },
    {
      key: 'activate',
      label: t('demo', 'batchActivate'),
      icon: <PoweroffOutlined />,
      type: 'primary',
      action: async (selectedRows: DemoRecord[]) => {
        console.log(
          '批量激活:',
          selectedRows.map((r) => r.id),
        );
      },
    },
    {
      key: 'export',
      label: t('demo', 'batchExport'),
      icon: <DownloadOutlined />,
      action: async (selectedRows: DemoRecord[]) => {
        console.log('导出数据:', selectedRows);
      },
    },
  ];

  // 使用配置构建器创建页面配置
  const templateConfig = createPageTemplateConfig<DemoRecord>()
    .setPageConfig({
      breadcrumb: {
        items: [
          {
            title: '首页',
            href: '/',
          },
          {
            title: '工作台',
            href: '/workBench',
          },
          {
            title: 'Demo 演示',
          },
        ],
      },
    })
    .setSearchConfig({
      fields: pageConfig.searchFields,
      initialValues: {
        status: 'active',
      },
    })
    .setTableConfig({
      columns: pageConfig.columns,
      rowKey: 'id',
      props: {
        scroll: { x: 1000 },
        size: 'middle',
      },
    })
    .setFormConfig({
      fields: pageConfig.formFields,
    })
    .setActionConfig({
      permissions: {
        create: true,
        update: true,
        delete: true,
        view: true,
      },
      customActions,
      onAction: handleAction,
    })
    .setBatchActionConfig({
      enabled: true,
      renderCustom: (selectedRowKeys, selectedRows) =>
        buildBatchActions(selectedRowKeys, selectedRows, batchActionConfigs),
    })
    .setCrudConfig(pageConfig.crudConfig)
    .build();

  return <PageTemplate<DemoRecord> {...templateConfig} />;
};

export default ConfigDemo;
